﻿using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Core.Validation
{
    public class DaValidationSummary : ValidationSummary
    {
        public DaValidationSummary()
        {
            this.Attributes.Add("data-valmsg-summary", "true");
        }

        protected override void Render(System.Web.UI.HtmlTextWriter writer)
        {

            ValidatorCollection validators = Page.Validators;

            // We have validators
            ArrayList errors = new ArrayList(validators.Count);
            for (int i = 0; i < validators.Count; i++)
            {
                if (!validators[i].IsValid)
                {
                    errors.Add(validators[i].ErrorMessage);
                }
            }

            bool hasErrors = errors.Count > 0;
            
            if (hasErrors)
            {
                writer.Write("<div data-valmsg-summary='true' class='validation-summary-errors'>");
                writer.Write("<span>" + this.HeaderText + "</span>");
                writer.Write("<ul>");
                foreach (object t in errors)
                {
                    writer.Write("<li>" + t + "</li>");
                }
                writer.Write("</ul>");
                writer.Write("</div>");
            }
            else
            {
                writer.Write("<div data-valmsg-summary='true' class='validation-summary-valid'>");
                writer.Write("<span>" + this.HeaderText + "</span>");
                writer.Write("<ul><li></li></ul>");
                writer.Write("</div>");
            }
        }

    }
}
